/**
 * 
 */
package org.jboss.minstaller

import org.jboss.minstaller.util.*;


/**
 * @author jjeon@redhat.com
 *
 */
public class ServerPrepareStage {
    def serverConfig; 
    def antBuilder = new AntBuilder();
    def jb29stringReplacer;
    def instanceNumber;
    
    public ServerPrepareStage(config) {
        serverConfig = config;
        jb29stringReplacer = new org.jboss.minstaller.util.StringReplacer(serverConfig.getWorkingDir());
    }
    
    public void makeWorkingDir() {
        antBuilder.mkdir(dir: serverConfig.getWorkingDir() );
    }
    
    public void cleanWorkingDir() {
        antBuilder.delete(dir: serverConfig.getWorkingDir() );
    }
    
    public void copyResources() {
        antBuilder.copy(todir:serverConfig.getWorkingDir()) {
            fileset(dir:"./resources/jbossas/" + serverConfig.getJBossVersion() + "/server/" + serverConfig.getLocalServerType(), includes:"**")
        }
    }
    
    public void replaceVariables(serverip, jbossServer) {
        jb29stringReplacer.replace("{###INSTANCE_NUMBER###}", instanceNumber + 1); // "/svc/uvls/was/jboss-eap-4.3/domains")
        
        jb29stringReplacer.replace("{###JBOSS_HOME###}", serverConfig.getServerJBossHome() ); //"/svc/uvls/was/jboss-eap-4.3/jboss-as")
        jb29stringReplacer.replace("{###SERVER_HOME###}", serverConfig.getServerServerHome() ); // "/svc/uvls/was/jboss-eap-4.3/domains")
        jb29stringReplacer.replace("{###SERVER_NAME###}", jbossServer.servername.text() ); //"clsboSrv11")
        
        jb29stringReplacer.replace("{###SERVER_IP###}", serverip ); //"clsboSrv11")
        
        jb29stringReplacer.replace("{###PARTITION_NAME###}", jbossServer.paritionname.text() );  //"clusterClsbo")
        jb29stringReplacer.replace("{###MULTICAST_ADDR###}", jbossServer.multicastaddr.text() ); // "228.1.1.1")
        
        jb29stringReplacer.replace("{###JBM_DC_PORT###}", jbossServer.JBM_DC_PORT.text() ); // "11011")
        jb29stringReplacer.replace("{###JBM_CC_PORT###}", jbossServer.JBM_CC_PORT.text() ); // "11021")
        
        jb29stringReplacer.replace("{###PORT_BINDING_NAME###}", jbossServer.port_binding_name.text() ); // "ports-01")
        jb29stringReplacer.replace("{###AJP_PORT###}", jbossServer.ajp_port.text() ); // "8109")
        
        jb29stringReplacer.replace("{###JNP_PORT###}", jbossServer.jnp_port.text() ); // "1199")
        jb29stringReplacer.replace("{###JBM_SERVERPEERID###}", jbossServer.JBM_SERVERPEERID.text() ); // "0")
        
        jb29stringReplacer.replace("{###APP_HOME###}", serverConfig.getAppHome() ); // "0")
        
        jb29stringReplacer.replace("{###APP_DIRECTORY###}", serverConfig.getAppHome() + "/" + jbossServer.webapp.text() + "/" ); // "0")
        
        jb29stringReplacer.replace("{###ADMIN_USERID###}", serverConfig.getServerUserid() ); //"uvlsadmin")
        jb29stringReplacer.replace("{###ADMIN_PASSWD###}", serverConfig.getServerPasswd() ); // "w!@34%^")             
    }
    
    public void prepare(instanceNum, serverip, jbossServer) {
        instanceNumber = instanceNum;
        cleanWorkingDir();
        makeWorkingDir();
        copyResources();
        replaceVariables(serverip, jbossServer);	
    }
    
    static void main(args) {
        ServerPrepareStage prepareStage = new ServerPrepareStage(new ServerConfig());
        prepareStage.cleanWorkingDir();
        prepareStage.makeWorkingDir();
        prepareStage.copyResources();
        prepareStage.replaceVariables();
    }
}
